<?php
namespace App\User\Http\Controller\Wap;

use App\ApiBaseController;
use App\Rules\IdCardRule;
use App\Rules\PhoneRule;
use App\User\Services\ArchiveService;
use Exception;
use Illuminate\Support\Facades\Validator;

class ArchiveController extends ApiBaseController
{

    /**
     * User: Virus
     * DateTime: 2021/6/21 10:06
     * Describe: 表单验证
     * Version: v1.0.0
     * @throws Exception
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        $rule = [
            'id' => 'required|integer',
            'card_type' => 'required|integer',
            'id_card' => ['required', new IdCardRule()],
            'sex' => 'required|integer',
            'birthday' => 'required|date',
            'age' => 'required|integer',
            'tel' => ['required', new PhoneRule('联系')],
            'home_province' => 'required|integer',
            'home_city' => 'required|integer',
            'home_county' => 'required|integer',
            'home_detail' => 'required',
            'home_postcode' => 'required|integer',
            'work_tel' => [new PhoneRule('单位')],
            'contact_tel' => [new PhoneRule('联系人')]
        ];

        $message = [
            'id.required' => '缺少用户ID',
            'id.integer' => '用户ID格式错误',
            'card_type.required' => '请选择证件类型',
            'card_type.integer' => '证件类型格式不正确',
            'id_card.required' => '请输入证件号码',
            'sex.required' => '请输入年龄',
            'sex.integer' => '年龄格式不正确',
            'birthday.required' => '请选择出生日期',
            'birthday.date' => '出生日期格式错误',
            'age.required' => '请输入年龄',
            'age.integer' => '年龄格式不正确',
            'tel.required' => '请输入手机号',
            'home_province.required' => '请选完善现住地址',
            'home_province.integer' => '现住地址格式不正确',
            'home_city.required' => '请选完善现住地址',
            'home_city.integer' => '现住地址格式不正确',
            'home_county.required' => '请选完善现住地址',
            'home_county.integer' => '现住地址格式不正确',
            'home_detail.required' => '请选完善现住地址',
            'home_postcode.required' => '请填写现住地址邮编',
            'home_postcode.integer' => '现住地址邮编格式不正确',
        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));
        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new Exception($validate->errors()->first());
        }
        return true;
    }



    /**
     * User: Virus
     * DateTime: 2021/6/21 19:08
     * Describe: 获取列表
     * Version: v1.0.0
     */
    public function index()
    {
        $data = request()->input();
        $limit = request()->input('limit', 10);
        try {
            $service = new ArchiveService();
            $res = $service->getList($data, $limit);
            return $this->success($res);
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }

    }

    /**
     * User: Virus
     * DateTime: 2021/6/21 10:00
     * Describe: 个人建档
     * Version: v1.0.0
     */
    public function create()
    {
        $param = request()->input();

        // 表单验证
        try {
            $rules = [ 'card_type', 'id_card', 'sex', 'birthday', 'age', 'tel', 'work_tel', 'contact_tel',
                'home_province', 'home_city', 'home_county', 'home_detail', 'home_postcode'];
            $this->validate($param, $rules);

            $service = new ArchiveService();
            $res = $service->create($param);
            return $this->success('success', $res);
        }catch (Exception $exception){
            return $this->error($exception->getMessage());
        }
    }


    /**
     * User: Virus
     * DateTime: 2021/6/21 15:49
     * Describe: 获取个人档案详情
     * Version: v1.0.0
     */
    public function info()
    {
        $condition = request()->input('condition', false);
        try {
            $service = new ArchiveService();
            $res = $service->getInfo($condition);
            if(!$res){
                throw new Exception('个人档案不存在！');
            }
            return $this->success($res);
        }catch (Exception $exception){
            return $this->error($exception->getMessage());
        }
    }


    /**
     * User: Virus
     * DateTime: 2021/6/21 10:00
     * Describe: 个人建档
     * Version: v1.0.0
     */
    public function update()
    {
        $param = request()->input();

        // 表单验证
        try {
            $rules = [ 'id', 'card_type', 'id_card', 'sex', 'birthday', 'age', 'tel', 'work_tel', 'contact_tel',
                'home_province', 'home_city', 'home_county', 'home_detail', 'home_postcode'];
            $this->validate($param, $rules);

            $service = new ArchiveService();
            $res = $service->update($param);
            return $this->success('success', $res);
        }catch (Exception $exception){
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: Virus
     * DateTime: 2021/6/22 13:12
     * Describe: 删除个人档案
     * Version:
     */
    public function delete($id)
    {
        try {
            $service = new ArchiveService();
            $res = $service->delete($id);
            return $this->success('删除成功！');
        }catch (Exception $exception){
            return $this->error($exception->getMessage());
        }
    }


}
